<template>
	<view class="content">
		<u-navbar title="我的位置" :autoBack="true" :placeholder="true"></u-navbar>
		<view class="page-body">
			<view class="page-section page-section-gap">
				<view class="mapsearch" :style="{top:(gap+40)+'px'}">
					<view class="text">内江</view>
					<text class="iconfont icon-xiala"></text>
					<u-search @change="inputadd" placeholder="请输入您的地址" v-model="keyword" :clearabled="true" :showAction="false" height="26"></u-search>
				</view>
				<scroll-view scroll-y="true" class="option" v-show="IsOption">
					<view class='column_item' v-for='item in data' :key='item.id' @click="tapOption(item.location)" >
						<view class="left">
							<text>{{item.title}}</text>
							<view class="address">{{item.address}}</view>
						</view>
						<view class="rigth">{{item.dist?item.dist>=1000?(item.dist / 1000).toFixed(1)+'KM':item.dist+'M':''}}</view>
					</view>
				</scroll-view>
				<map id="map" ref="map" style="width: 100%; height: 730rpx;" v-show="!IsOption" :latitude="latitude"
				 :longitude="longitude" :markers="covers" @regionchange="changeMap">
				 <cover-image class="img-map1" src="https://xixijj.oss-cn-chengdu.aliyuncs.com/store/comment/303a7202309191143075006.png"></cover-image>
				 <cover-image class="img-map2"  src="../static/icon/again.png" @click="clickcontrol"></cover-image>
				</map>
			</view>
		</view>
		<scroll-view class="sy-y" scroll-y="true" >
			<view class="nearc-list">
				<view class="naerc-item" v-for="item in pois" :key="item.id" @click="seletbtn(item,item.location)">
				<text class="iconfont icon-address"></text>
				{{item.title}}
				<view class="address">{{item.address}}</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import _ from 'lodash';
	import { mapState } from 'vuex'
	import qqmap from '@/utils/qqmap.js'
	import distance from '@/api/distancematrix.js'
	export default {
		data() {
			return {
				title: 'map',
				latitude: null,
				longitude: null,
				data:[],
				covers: [],
				pois: [],
				state:null,
				keyword:'',
				IsOption:false,
				gap:0
			}
		},
		watch:{
			keyword: _.debounce(function(newKeyword, oldKeyword){
				if(!newKeyword){
				  return ''
				}else{
				this.data = [];
				this.addresssearch();
				}
			},1000)
		},
		methods: {
			position() {
				uni.getLocation({
					type:'gcj02',
					isHighAccuracy: true,
					altitude: true,
					success: (res) => {
						this.latitude = res.latitude
						this.longitude = res.longitude
						this.markpost(res.latitude, res.longitude)
					},
					fail: (err) => {
						console.log('获取地址失败' + err);
					}
				})
			},
			markpost(lat, lng) {
				qqmap.reverseGeocoder({
					location: {
						latitude: lat,
						longitude: lng
					},
					get_poi: 1,
					success: (res) => {
						this.pois = res.result.pois
					},
					fail: (error) => {
						console.error(error);
					},
				})
			},
			clickcontrol(){
				uni.createMapContext("map", this).moveToLocation({
				        latitude: this.latitude,
				        longitude: this.longitude
				})
			},
			changeMap(e){
				let type = ''
				const { platform } = uni.getSystemInfoSync() 
				switch (platform) {  
					case 'android':  
						type = e.type
						break; 
					case 'ios':  
						type = e.detail.type 
						break;  
				}
				if (type === 'end') {  
					// 处理拖拽结束时的逻辑  
					let mapCtx =uni.createMapContext('map',this);
					 mapCtx.getCenterLocation({
					  success:res=> {
						// 当前中心经纬度位置
						this.markpost(res.latitude,res.longitude)
					  }
					})
				}
			},
			addresssearch(){
				qqmap.search({
					keyword:this.keyword, //搜索关键词
					 success: res =>{
						res.data.forEach(el => {
							this.data.push({
								id:el.id,
								address:el.address,
								location:el.location,
								title:el.title
							})
						})
						this.distanceapi()
					 }
				})
				this.IsOption = true
			},
			inputadd(){
				if(!this.keyword){
				this.IsOption = false;
				this.data = [];
				}
			},
			seletbtn(item,location) {
				this.$bus.$emit('backWithParams', { param: item.address+item.title,position: location })
				uni.navigateBack({
					delta:1,
				})
			},
			tapOption(post){
				this.latitude = post.lat
				this.longitude = post.lng
				this.markpost(post.lat,post.lng)
				this.keyword = ''
			},
			async distanceapi(){
				let distanlist = []
				const fromlist = this.data.map(el => el.location)
				const from = fromlist.slice(0,5).map(item => `${item.lat},${item.lng}`).join(';')
				const to = `${this.latitude},${this.longitude}`
				const res = await distance(from,to)
				distanlist.push(...res.result.rows)
				if(fromlist.length>5){
					setTimeout( async() =>{
						let i = 0
						const formend = fromlist.slice(5).map(item => `${item.lat},${item.lng}`).join(';')
						const eforom = await distance(formend,to)
						distanlist.push(...eforom.result.rows)
						this.data.forEach(obj =>{
							this.$set(obj, 'dist', distanlist[i].elements[0].distance);
							i++
						})
					},1000)
				}else{
					let i = 0
					this.data.forEach(obj =>{
						this.$set(obj, 'dist', distanlist[i].elements[0].distance);
						i++
					})
				}
			}
		},
		mounted() {
		},
		onLoad(el) {
			this.position()
			this.state = el.type
			this.gap = uni.getSystemInfoSync().statusBarHeight
		}
	}
</script>

<style lang="scss">
	.sy-y{
		height:730rpx;
		padding-bottom: 50rpx;
	}
	.nearc-list {
		padding-left: 30rpx;
		margin-top: 30rpx;
		.naerc-item {
			position: relative;
			padding-bottom: 30rpx;
			margin-top: 20rpx;
			border-bottom: 1px dashed #E6E6E6;
			.icon-address{
				position: absolute;
				right: 40rpx;
				top: 30rpx;
			}
			.address{
				font-size: 22rpx;
				color: #999;
			}
		}
	}
	.page-section{
		position: relative;
	}
	.mapsearch{
		box-sizing: border-box;
		width: 100%;
		position: fixed;
		font-size: 26rpx;
		display: flex;
		padding: 20rpx 30rpx;
		align-items: center;
		background-color: #fff;
		z-index: 999;
		.icon-xiala{
			font-size: 26rpx;
			margin-right: 20rpx;
		}
	}
	.option{
		box-sizing: border-box;
		position: absolute;
		padding:0 20rpx;
		height: 100vh;
		z-index: 999;
		background-color: #fff;
		.column_item{
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 28rpx;
			padding: 20rpx 0;
			background-color: #fff;
			border-bottom: 1rpx solid #ebebeb;
			.left{
				.address{
					width: 600rpx;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
					font-size: 22rpx;
					margin-top: 4rpx;
					color: #666;
				}
			}
			.rigth{
				font-size: 22rpx;
				color: #949494;
			}
		}
	}
	.img-map1{
		position: absolute;
		width: 50rpx;
		height: 60rpx;
		top: 34%;
		left: 46%;
	}
	.img-map2{
		width: 50rpx;
		height: 50rpx;
		position: fixed;
		top: 50vh;
		right: 1.5vh;
		background-color: #FFFFFF;
		border-radius: 5px;
	}
</style>